<?php

/**
 * @author Samrat Khan
 * @author Zorj
 * Sep 26, 2013 - 10:24:23 AM
 */
ob_start();
$host = 'localhost';
$user = 'root';
$pass = 'samraj77';
$db = 'final_warehouse';
//$table = 'stockin';
$file = 'MTR';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$values = mysql_query("
       SELECT t1.serialno,
            (SELECT projectname FROM projects WHERE t1.project_id = projects.project_id) AS ProjectName,
            t1.productid,
            t1.description,
            t1.revisionstate, 
            t1.in_quantity,
            (SELECT measurement FROM uom WHERE t1.uom = uom.uom_id) AS UOM,
            (SELECT typename FROM shiptype WHERE t1.shiptype = shiptype.shiptype_id) AS ShipType,
            t1.invoicenumber,
            t1.shipid,
            t1.mawb,
            t1.hawb,
            t1.receiveddate,
            t1.shipfrom,
            t1.boxno,
            t1.rma,
            (SELECT warehousename FROM warehouses WHERE t1.warehouse_id = warehouses.warehouse_id) AS WarehouseName,
            t1.rackno,
            t1.docketnumber,
            t1.remarks, 
            (SELECT username FROM wms_users WHERE t1.userid = wms_users.id) AS UserName,
            (SELECT cusname FROM customerinfo WHERE t1.cus_id = customerinfo.cus_id) AS CustomerName,
            t1.inserteddate
        FROM stockin AS t1 WHERE shiptype = '8' AND junkentry = '1'
        ");
$headings = array( 'SerialNo', 'Project Name','ProductID','Description','Revision State','In Quantity','UOM','Ship Type','Invoice Number','Ship Number','MAWB','HAWB','Received Date','Ship From','Box No','RMA','Warehouse Name','Rack No','Docket Number','Remarks','Username','Customer Name','Inserted Date');
$i = 0;
foreach ($headings as $heading) {
    $csv_output .= $heading . ", ";
    $i++;
}
$csv_output .= "\n";
while ($rowr = mysql_fetch_row($values)) {
for ($j = 0; $j < $i; $j++) {
        $csv_output .= '"'.$rowr[$j] . '",';
    }
$csv_output .= "\n";
}
ob_end_clean();



$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
//header("Content-type: text/csv");
header('Content-Length: ' . strlen($csv_output) );
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
